/**
 * All rights Reserved, Designed By 溪云阁
 * Copyright:    Copyright(C) 2016-2020
 */

package com.module.boots.coder.base;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.velocity.VelocityContext;

import com.module.boots.coder.utils.ColumnUtils;

/**
 * 基础生成类
 * @author：溪云阁
 * @date：2020年9月4日
 */
public class BaseCoder {

    /***包路径***/
    private static final String PACKAGE_PATH = "src/main/java/com/lemone/";

    /***包名称***/
    private static final String PACKAGE_NAME = "com.lemone.";

    /***资源路径***/
    private static final String PATH_RESOURCES = "src/main/resources/mappers/";

    // 项目名称,例如com.lemone.security,此时security就是项目名称
    public static String projectName;

    // 表名
    public static String tableName;

    // 表说明
    public static String tableDescription;

    // 作者
    public static String author;

    /**
     * 初始化基础生成信息
     * @author 溪云阁
     * @param projectName
     * @param tableName
     * @param tableDescription
     * @param author void
     */
    public static void setBaseInfo(String projectName, String tableName, String tableDescription, String author) {
        BaseCoder.projectName = projectName;
        BaseCoder.tableName = tableName;
        BaseCoder.tableDescription = tableDescription;
        BaseCoder.author = author;
    }

    /**
     * 获取最基础XML的路径
     * @author 溪云阁
     * @return String
     */
    public String getBaseXMLPath() {
        return PATH_RESOURCES;
    }

    /**
     * 获取类名,第一个大写
     * @author 溪云阁
     * @param tableName
     * @return String
     */
    public String getUpperClassName(String tableName) {
        return ColumnUtils.formatUnderlineNameFirstNameUpper(tableName);
    }

    /**
     * 获取最基础包路径
     * @author 溪云阁
     * @return String
     */
    public String getBasePackagePath() {
        return PACKAGE_PATH + projectName.toLowerCase();
    }

    /**
     * 获取最基础包名称
     * @author 溪云阁
     * @return String
     */
    public String getBasePackageName() {
        return PACKAGE_NAME + projectName.toLowerCase();
    }

    /**
     * 获取最基础模板
     * @author 溪云阁
     * @param tableName
     * @return VelocityContext
     */
    public VelocityContext getBaseContext(String tableName) {
        // 获取类名,第一个大写
        final String upperClassName = ColumnUtils.formatUnderlineNameFirstNameUpper(tableName);

        // 获取类名,第一个小写
        final String lowerClassName = ColumnUtils.formatUnderlineNameFirstNameLower(tableName);
        // 获取类名,全部小写
        final String alllowerCalssName = lowerClassName.toLowerCase();
        final VelocityContext context = new VelocityContext();
        context.put("basePackageName", getBasePackageName());
        context.put("createDate", dateToString(new Date()));
        context.put("author", author);
        context.put("tableName", tableName);
        context.put("upperClassName", upperClassName);
        context.put("lowerClassName", lowerClassName);
        context.put("alllowerCalssName", alllowerCalssName);
        context.put("tableDescription", tableDescription);
        return context;
    }

    /**
     * 获取时间
     * @author 溪云阁
     * @param time
     * @return String
     */
    public String dateToString(Date time) {
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("yyyy年MM月dd日");
        final String ctime = formatter.format(time);
        return ctime;
    }

}
